package cn.iocoder.yudao.module.promotion.controller.admin.prizedraw;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.promotion.controller.admin.prizedraw.vo.*;
import cn.iocoder.yudao.module.promotion.service.prizedraw.PrizeDrawService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 抽奖活动")
@RestController
@RequestMapping("/promotion/prize-draw")
@Validated
public class PrizeDrawController {

    @Resource
    private PrizeDrawService prizeDrawService;

    @Operation(summary = "抽奖活动库存")
    @PreAuthorize("@ss.hasPermission('promotion:prize-draw:query')")
    @Parameter(name = "prizeDrawId", description = "抽奖活动ID", required = true, example = "1024")
    @GetMapping("/getStore/{prizeDrawId}")
    public CommonResult<List<PrizeDrawStoreRespVo>> getStore(@PathVariable Long prizeDrawId) {
        return success(prizeDrawService.getStore(prizeDrawId));
    }

    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('promotion:prize-draw:query')")
    @Operation(summary = "抽奖活动分页列表")
    public CommonResult<PageResult<PrizeDrawRespVO>> getPrizeDrawPage(@Valid PrizeDrawPageReqVO pageReqVO) {
        return success(prizeDrawService.getPrizeDrawPage(pageReqVO));
    }

    @Operation(summary = "查询抽奖活动详情信息")
    @Parameter(name = "prizeDrawId", description = "抽奖活动ID", required = true, example = "1024")
    @GetMapping("/getInfoById")
    @PreAuthorize("@ss.hasPermission('promotion:prize-draw:query')")
    public CommonResult<PrizeDrawRespVO> getInfoById(@NotNull(message = "抽奖活动Id不能为空") @RequestParam(value = "prizeDrawId") Long prizeDrawId) {
        return success(prizeDrawService.getPrizeDraw(prizeDrawId));
    }

    @PostMapping
    @Operation(summary = "创建抽奖活动")
    @PreAuthorize("@ss.hasPermission('promotion:prize-draw:create')")
    public CommonResult<Boolean> create(@Valid @RequestBody PrizeDrawSaveReqVO createReqVO) {
        prizeDrawService.createPrizeDraw(createReqVO);
        return success(true);
    }

    @PutMapping
    @Operation(summary = "更新抽奖活动")
    @PreAuthorize("@ss.hasPermission('promotion:prize-draw:update')")
    public CommonResult<Boolean> edit(@Valid @RequestBody PrizeDrawSaveReqVO updateReqVO) {
        prizeDrawService.updatePrizeDraw(updateReqVO);
        return success(true);
    }


    @PutMapping("/chance")
    @Operation(summary = "修改抽奖活动概率")
    @PreAuthorize("@ss.hasPermission('promotion:prize-draw:chance')")
    public CommonResult<Boolean> chance(@Valid @RequestBody PrizeDrawChanceReqVO chanceReqVO) {
        prizeDrawService.chancePreserve(chanceReqVO);
        return success(true);
    }

    @PostMapping("/updown")
    @Operation(summary = "启停")
    @PreAuthorize("@ss.hasPermission('promotion:prize-draw:updown')")
    public CommonResult<Boolean> updown(@Valid @RequestBody PrizeDrawUpdownReqVO updownReqVO) {
        prizeDrawService.updown(updownReqVO);
        return success(true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除")
    @PreAuthorize("@ss.hasPermission('promotion:prize-draw:delete')")
    public CommonResult<Boolean> delete(@Valid @RequestBody PrizeDrawDeleteReqVO deleteReqVO) {
        prizeDrawService.delete(deleteReqVO);
        return success(true);
    }
}